layui.use(['form','table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 分类商品管理
     */
    var FlowGoods = {
        tableId: "flowGoodsTable"
    };

    /**
     * 初始化表格的列
     */
    FlowGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'goodsId', hide: true, title: 'ID'},
            {field: 'goodsName', sort: true, title: '物品名称'},
            {field: 'typeName', sort: true, title: '物品分类'},
            {field: 'cover', sort: true, title: '物品图片',templet:function (row) {
                    return row.cover ? "<img src='"+row.cover+"' />" : "无";
            }},
            {field: 'goodsPrice', sort: true, title: '费用'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowGoods.search = function () {
        var queryData = {};

        queryData['goodsName'] = $('#goodsName').val();
        queryData['goodsTypeId'] = $("#goodsTypeId").val();
        table.reload(FlowGoods.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      FlowGoods.openEditDlg = function (data) {
          func.open({
              title: '修改商品价格',
              content: Feng.ctxPath + '/flowStoreCity/flowGoodsPriceEdit?goodsId=' + data.goodsId+"&storeCityId="+Feng.getUrlParam("storeCityId"),
              tableId: FlowGoods.tableId
          });
      };
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FlowGoods.search();
    });


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowGoods.tableId,
        url: Feng.ctxPath + '/flowStoreCity/flowGoodsList?storeCityId='+Feng.getUrlParam("storeCityId"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowGoods.initColumn()
    });

    // 工具条点击事件
    table.on('tool(' + FlowGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FlowGoods.openEditDlg(data);
        }
    });

    //查询分类
    //类型
    var goodTypeAjax = new $ax(Feng.ctxPath + "/flowStoreCity/goodsType");
    var goodTypeResult = goodTypeAjax.start();
    if(goodTypeResult.code == 200){
        var goodTypeStr = "<option value=''>请选择物品分类</option>";
        for (var i = 0;i<goodTypeResult.data.length;i++){
            goodTypeStr += "<option value='"+goodTypeResult.data[i].goodsTypeId+"'>"+goodTypeResult.data[i].typeName+"</option>";
        }
        $("#goodsTypeId").html(goodTypeStr);
        form.render('select');
    }
});
